package Tools;

import java.util.ArrayList;

import Appconst.GameConst;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
public  class GameButton implements GameButtonListner{

	private Bitmap img_button_bg; //关卡背景图
	private Bitmap  img_button_front;// 关卡数字图片或者是 X(未打开)图片
	private boolean showFrontBG; //是否显示前景
	private int id; //GAMEBUTTON--ID
	private float x; //背景button的X
	private float y; //背景button的Y
	private float width; 
	private float height;
	private boolean isVisible;//是否显示
	private boolean canTouched ;
	private Rect buttonRect;
	
	//Button 伸展效果
	private boolean showEffect; //是否有效果
	public final static int HORISON_EFFECT = 1; //效果ID
	public final static int VERTICAL_EFFECT = 2;//
	private  int effectID = 0; //第几种效果
	private int clipH = 1;
	private int clipW = 1;
	//设置  背景缩放大小
	private float scaleBgW;
	private float scaleBgH;
	 //设置 前景缩放大小
	private float scaleFrontW;
	private float scaleFrontH;
	
	//保存初始化Button位置
	private int org_posX;
	private int org_posY;
	
	 
	 //BUtton绘制关卡数字
	 private int levelNumber = -1;//默认为未打开
	 
	 
	 
	/**
	 * 
	 * @param id 索引号
	 * @param c  
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public GameButton(int id,int x,int y,int w,int h)
	{
		this.id = id;
		isVisible = true;
		this.setX(x);
		this.setY(y);
		width = w;
		height = h;
		setButtonRect(new Rect());
		x = getButtonRect().left;
		y = getButtonRect().top;
		w = getButtonRect().right-getButtonRect().left;
		h = getButtonRect().bottom-getButtonRect().top;
		canTouched = true;
		
		setOrg_posX(x);
		setOrg_posY(y);
		
		setShowFrontBG(false);
		setGameListener(this);
	}
	public GameButton(int id,Bitmap bg,int x,int y,int w,int h)
	{
		this.id = id;
		this.img_button_bg = bg;
		isVisible = true;
		this.setX(x);
		this.setY(y);
		width = w;
		height = h;
		canTouched = true;
		setShowFrontBG(false);
		
		setScaleBgW(getWidth());
		setScaleBgH(getHeight());
		setOrg_posX(x);
		setOrg_posY(y);
			
		setGameListener(this);
	}
	
	public GameButton(int id,Bitmap bg,Bitmap frontbg,int x,int y,int w,int h)
	{
		this.id = id;
		this.img_button_bg = bg;
		this.img_button_front = frontbg;
		isVisible = true;
		this.setX(x);
		this.setY(y);
		width = w;
		height = h;
		canTouched = true;
		setShowFrontBG(true);
		
		setScaleBgW(getWidth());
		setScaleBgH(getHeight());
		setScaleFrontW(frontbg.getWidth());
		setScaleFrontH(frontbg.getHeight());
		
		setOrg_posX(x);
		setOrg_posY(y);
		
		setGameListener(this);
	}
	
	
	public GameButton setButton_BG(Bitmap bg)
	{
		this.img_button_bg = bg;
		return this;
	}
	public Bitmap getButton_BG()
	{
		return this.img_button_bg;
	}
		
	public GameButton setSize(int w,int h)
	{
		width = w;
		height = h;
		return this;
	}
	public float getPosx()
	{
		return getX();
	}
	public float getPosy()
	{
		return getY();
	}
	public float getWidth() //Button背景的宽
	{
//		if(getScaleBgW() != width)
//			return getScaleBgW();
//		else
			return width;
	}
	public float getHeight() ////Button背景的高
	{
//		if(getScaleBgH() != height)
//			return getScaleBgH();
//		else
			return height;
	}
	public float getWidthMidlle()
	{
		return getX() + width/2;
	}
	public float getHeightMidlle()
	{
		return getY() + height/2;
	}
	public int getId()
	{
		return id;
	}
	
	public void setId(int id)
	{
		this.id = id;
	}
	
	public GameButton setVisible(boolean v)
	{
		isVisible = v;
		return this;
	}
	public boolean getVisible()
	{
		return isVisible;
	}
	
	
	 public void setStrentchEffectHV(Canvas canvas) {
		 
		 if(!isShowEffect())
			 return;
		 
		 	int speed = 8;
	    	int org_imgH = img_button_bg.getHeight(); 
	    	int org_imgW = img_button_bg.getWidth();
	    	int half_orgH = org_imgH>>1;
	        int half_orgW = org_imgW>>1;
	    	if(effectID == VERTICAL_EFFECT)
	    	{
	    		canvas.clipRect(getX(),getY()+half_orgH-getClipH()/2,getX()+org_imgW,getY()+half_orgH-getClipH()/2+getClipH());
//	        	g.setClip(x, y+half_orgH-getClipH()/2, org_imgW, getClipH());
	        
	        	if(getClipH()<=org_imgH)
	        		setClipH(getClipH() + speed);	
	    	}
	    	else if(effectID == HORISON_EFFECT)
	    	{
	    		canvas.clipRect(getX()+half_orgW-getClipW()/2, getY(), getX()+half_orgW-getClipW()/2+getClipW(), getY()+org_imgH);
//	        	g.setClip(x+half_orgW-getClipW()/2, y, getClipW(), org_imgH);
	        
	        	if(getClipW()<=org_imgW)
	        		setClipW(getClipW() + speed);	
	    	}
		}
	
	
	public void onDraw(Canvas canvas,Paint paint)
	{
		canvas.save();
		if(isVisible)
		{
			if(isShowEffect())
				setStrentchEffectHV(canvas);
			else 
				canvas.clipRect(0, 0, GameConst.SCR_W, GameConst.SCR_H);
//				g.setClip(0, 0, GameConst.SCR_W, GameConst.SCR_H);
			

			BitmapTool.drawScale(canvas, paint, img_button_bg, getScaleBgW(), getScaleBgH(),getX(), getY());
//			canvas.drawBitmap(img_button_bg, x, y, paint);
			
			if(isShowFrontBG())
				drawFrontCenter(canvas,paint);
			
		}
		canvas.restore();
	}
	
	public void LocateOrgPostion()
	{
		this.setX(getOrg_posX());
		this.setY(getOrg_posY());
	}
	
//	public boolean onButtonMove(float touchX,float touchY)
//	{}
	
//	public boolean onButtonLeave(float touchX,float touchY)
//	{
//		if(!isCanTouched())
//			return false;
//		
//		if(touchX>getPosx()&&touchX<getPosx()+getWidth()&&touchY>getPosy()&&touchY<getPosy()+getHeight())
//		{
//			return true;
//		}
//		else {
//			return false;
//		}
//		return false;
//	}
	
//	public boolean onButtonPressed(float touchX,float touchY)
//	{}
	
	public void clearStrectchEffect()
	{
		setClipW(1);
		setClipH(1);
	}
	
	public void setClipW(int clipW) {
		this.clipW = clipW;
	}
	public int getClipW() {
		return clipW;
	}
	public void setClipH(int clipH) {
		this.clipH = clipH;
	}
	public int getClipH() {
		return clipH;
	}
	public GameButton setShowEffect(boolean showEffect) {
		this.showEffect = showEffect;
		return this;
	}
	public boolean isShowEffect() {
		return showEffect;
	}
	
	public GameButton setEffect_ID(int id)
	{
		this.effectID = id;
			return this;
	}
	public int getKind_effect() {
		return effectID;
	}
	
	
	
	
	/**
	 * 居中绘制关卡数字或者 未开启符号X
	 * @param g
	 * @param img
	 * @param levelNumber
	 */
	public  void drawFrontCenter(Canvas canvas,Paint paint)
	{
		float x = 0;
		float y = 0;
	
		if(getLevelNumber()==-1) //UnOpened 没有设置level的话就是-1
		{
			
			x = getPosx()+(getScaleBgW()-getScaleFrontW())/2;
			y = getPosy()+(getScaleBgH()-getScaleFrontH())/2;
				
			BitmapTool.drawScale(canvas, paint, img_button_front, getScaleFrontW(), getScaleFrontH(), x, y);
//			drawRegion(canvas,paint, x, y, img_button_front, 0, 0, img_button_front.getWidth(), img_button_front.getHeight());
		}
		else
		{
		if(levelNumber>=0&&levelNumber<10)
		{
			x = getPosx()+(getWidth())/3;
			y = getPosy()+(getHeight())/3;
		}
		else if(levelNumber>=10&&levelNumber<100){
			x = getPosx()+getWidth()/8;
			y = getPosy()+getHeight()/3;
		}
		drawLevelNum(canvas,paint, img_button_front, levelNumber, x, y);
		}
	}
	
	
	
	
	/**
	 * 绘画图片的一部分。
	 * @param g
	 * @param x
	 * @param y
	 * @param img
	 * @param sx
	 * @param sy
	 * @param w
	 * @param h
	 * @param Aline 默认是0
	 */
    public final static void drawRegion(Canvas canvas,Paint paint,float x, float y, Bitmap img, float sx, float sy, float w, float h ){
    	canvas.clipRect(x, y, x+w, y+h);
//        g.setClip( x, y, w, h );
    	canvas.drawBitmap(img, x-sx, y-sy, paint);
//        g.drawImage( img, x-sx, y-sy, 20 );
    	canvas.clipRect(0, 0, GameConst.SCR_W, GameConst.SCR_H);
//        g.setClip( 0, 0, LlkSetting_240.SCREEN_W, LlkSetting_240.SCREEN_H);
    }
	
    /**
     * 绘制数字图片
     * @param g
     * @param img
     * @param levelNumber
     * @param x
     * @param y
     * @param Aline 默认为0
     */
	public final static void drawLevelNum(Canvas canvas,Paint paint,Bitmap img,int levelNumber,float x,float y)
	{
		if(splitBigNumberList.size() != 0) 
			splitBigNumberList.clear();
		
		if(splitBigNumberList.isEmpty())//尚未切割数字,只执行一次
			spitNumberMethodI(levelNumber);
		
		
		int number_imgW = 0;
		int number_imgH = 0;
		int offW = 0;
		number_imgW = img.getWidth();
		number_imgH = img.getHeight();
		offW = number_imgW/10;  //按数字分成10份【0-9】
		
			for (int i = 0; i < splitBigNumberList.size(); i++) {
				drawRegion(canvas,paint,x+offW*i, y, img, splitBigNumberList.get(i)*offW,0,offW,number_imgH);
		}
		
	}
	
	private static ArrayList<Integer> splitBigNumberList = new ArrayList<Integer>();
//	public final  static void spitNumber(int number)  //有BUG的 如果包含数字0
//	{
//		int counter = 0;
//		int temp = number;
//		if(temp<10)
//		{
//			splitBigNumberList.add(new Integer(temp));
//			return;
//		}
//		while (temp/10>0) {
//			temp = temp/10;
//			counter++;
//		}
//		int firstNumber = number / (int)Math.pow(10, counter);
//		splitBigNumberList.add(new Integer(firstNumber)); //821101
//		number -= firstNumber*(int)Math.pow(10, counter);
//		spitNumber(number);
//	}
	
	
	public static void spitNumberMethodI(int num){
		
		if(num>=0&&num<10)
		{
			splitBigNumberList.add(new Integer(num));
			return;
		}
		
		int yuNum = num % 10;
		int chuNum = num / 10;
		splitBigNumberList.add(new Integer(yuNum));
		if (chuNum >= 10) {
			spitNumberMethodI(chuNum);
		}else{
			splitBigNumberList.add(new Integer(chuNum));
			int count = splitBigNumberList.size();
			int halfNum = count / 2;
			for (int i = 0 ; i < halfNum ; i++){
				int tmp = splitBigNumberList.get(i).intValue();
				splitBigNumberList.set(i, splitBigNumberList.get(count - 1 - i).intValue());
				splitBigNumberList.set(count - 1 - i, new Integer(tmp));
			}
		}
	}
	
	public final  static void spitNumberMethodII(int number)
	{
		String str_num = number+"";
		for (int i = 0; i < str_num.length(); i++) {
			splitBigNumberList.add(new Integer(Integer.parseInt(str_num.charAt(i)+"")));
		}
	}
	
	public GameButton setLevelNumber(int levelNumber) {
		this.levelNumber = levelNumber;
		return this;
	}
	public int getLevelNumber() {
		return levelNumber;
	}
	public GameButton setButton_front(Bitmap img_button_front) {
		this.img_button_front = img_button_front;
		setShowFrontBG(true);
		return this;
	}
	public Bitmap getButton_front() {
		return img_button_front;
	}
	public void setShowFrontBG(boolean showFrontBG) {
		this.showFrontBG = showFrontBG;
	}
	public boolean isShowFrontBG() {
		return showFrontBG;
	}
	public GameButton setButtonRect(Rect buttonRect) {
		this.buttonRect = buttonRect;
		return this;
	}
	public Rect getButtonRect() {
		return buttonRect;
	}
	public GameButton setCanTouched(boolean canTouched) {
		this.canTouched = canTouched;
		return this;
	}
	public boolean isCanTouched() {
		return canTouched;
	}
	
	public GameButton setScaleBgW(float scaleBgW) {
		this.scaleBgW = scaleBgW;
		return this;
	}
	public float getScaleBgW() {
		return scaleBgW;
	}
	public GameButton setScaleBgH(float scaleBgH) {
		this.scaleBgH = scaleBgH;
		return this;
	}
	public float getScaleBgH() {
		return scaleBgH;
	}
	public GameButton setScaleFrontW(float scaleFrontW) {
		this.scaleFrontW = scaleFrontW;
		return this;
	}
	public float getScaleFrontW() {
		return scaleFrontW;
	}
	public GameButton setScaleFrontH(int scaleFrontH) {
		this.scaleFrontH = scaleFrontH;
		return this;
	}
	public float getScaleFrontH() {
		return scaleFrontH;
	}
	@Override
	public boolean OnButtonMoved(float touchX, float touchY) {

		System.out.println("GameButton.OnButtonMoved()pppppppppppppp");
		if(!canTouched)
			return false;
		
		setScaleBgW(getWidth()/2);
		setScaleBgH(getHeight()/2);
		
			this.setX(touchX -getWidth()/2);
//			System.out.println("moving --x ="+x);
			this.setY(touchY -getHeight()/2);
//			System.out.println("moving -- y ="+y);
		
			return true;
	}
	
	@Override
	public boolean OnButtonPressed(float touchX, float touchY) {

//		if(isShowFrontBG())
//		{
//			if(getLevelNumber() == -1) //有前景的境况下，判断是何种button  没有开通的关卡【没有设置LevelNumber】
//				return false;
//		}
		
		
		if(!isCanTouched())
			return false;
		
		if(touchX>getPosx()&&touchX<getPosx()+getScaleBgW()&&touchY>getPosy()&&touchY<getPosy()+getScaleBgH())
		{
			System.out.println("BUTTON--PRESSED--在触摸范围内");
			return true;
		}
		else {
			return false;
		}
	
		
	}
	@Override
	public boolean OnButtonLeaved(float touchX, float touchY) {
		if(!isCanTouched())
			return false;
		
		setScaleBgW(getWidth());
		setScaleBgH(getHeight());
		
		LocateOrgPostion();

		return true;
	}
	
	//监听器
	public  GameButtonListner listener;
	//开始修改
	public void setGameListener(GameButtonListner listener)
	{
		this.listener = listener;
		System.out.println("设置BUTTON监听！！！");
	}
	public void setOrg_posX(int org_posX) {
		this.org_posX = org_posX;
	}
	public int getOrg_posX() {
		return org_posX;
	}
	public void setOrg_posY(int org_posY) {
		this.org_posY = org_posY;
	}
	public int getOrg_posY() {
		return org_posY;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getX() {
		return x;
	}
	public void setY(float y) {
		this.y = y;
	}
	public float getY() {
		return y;
	}
	
}
